package com.example.tliaswebmanagement.controller;

import com.example.tliaswebmanagement.pojo.Clazz;
import com.example.tliaswebmanagement.pojo.ClazzQueryParam;
import com.example.tliaswebmanagement.pojo.PageResult;
import com.example.tliaswebmanagement.pojo.Result;
import com.example.tliaswebmanagement.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    private final ClazzService clazzService;

    public ClazzController(ClazzService clazzService) {
        this.clazzService = clazzService;
    }

    /**
     * 班级列表查询
     */
    @GetMapping
    public Result paginationClazz(ClazzQueryParam clazzQueryParam) {
        log.info("前端传递的参数：{}", clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.paginationClazz(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 新增班级信息
     */
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz) {
        log.info("前端传递的新增班级信息：{}", clazz);
        clazzService.addClazz(clazz);
        return Result.success();
    }

    /**
     * 根据 ID 查询班级（数据回显）
     */
    @GetMapping("/{id}")
    public Result getClazzInfo(@PathVariable Integer id) {
        log.info("前端要查询的班级ID：{}", id);
        Clazz clazz =  clazzService.getClazzInfo(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级
     */
    @PutMapping
    public Result updateClazzById(@RequestBody Clazz clazz) {
        log.info("前段传递的班级修改数据：{}", clazz);
        clazzService.updateClazzById(clazz);
        return Result.success();
    }

    /**
     * 删除班级
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("前端要删除的班级ID：{}", id);
        clazzService.deleteById(id);
        return Result.success();
    }

    /**
     * 查询所有班级
     */
    @GetMapping("/list")
    public Result findAllClazz() {
        List<Clazz> clazz = clazzService.findAllClazz();
        return Result.success(clazz);
    }

}
